package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.UserDao;
import com.daoimpl.UserDaoImpl;
import com.vo.User;

/**
 * Servlet implementation class ModifyUserServlet
 */
@WebServlet("/ForgetPasswordServlet")
public class ForgetPasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForgetPasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		// 设置响应内容的编码
		response.setContentType("text/html; charset=UTF-8");
	    // TODO 自动生成的方法存根
	    User newUser = new User();
	    newUser.setId((Integer.parseInt(request.getParameter("id"))));
	    newUser.setUsername(request.getParameter("username"));
	    newUser.setPassword(request.getParameter("password"));
	    newUser.setQuestion(request.getParameter("question"));
	    newUser.setAnswer(request.getParameter("answer"));
	    newUser.setType(request.getParameter("type"));

	    UserDao dao = new UserDaoImpl();

	    HttpSession session = request.getSession();
	    // 找到需要修改的用户
	    User oldUser = (User) session.getAttribute("userinfo");
	    if (dao.modifyUser(oldUser, newUser)) {
	    	session.removeAttribute("userinfo");
	    	response.getWriter().println("修改成功！");
	    } else {
	        // 修改失败
	        response.getWriter().println("修改失败！");
	    }
	    response.setHeader("refresh","0.5; URL=login.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
